package com.zhangyj.applets.data;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DBService {
	public static class MenuNode{
		public MenuNode(int sid,String description) {
			this.sid = sid;
			this.description = description;
		}
		public int sid;
		public String description;
		@Override
		public String toString() {
			return description;
		}
		
	}
    public static String contextpath = "";
    public static String jsessionid = "";
    private static URL url;

    public static ResultData send(ParamData param) {
        try {
            if (url == null) {
                url = new URL("http", "localhost", contextpath + "/appletServlet.app;jsessionid=" + jsessionid);
            }
            URLConnection connect = url.openConnection();
            connect.setDoOutput(true);
            connect.setUseCaches(false);
            connect.setRequestProperty("Content-Type", "application/octet-stream");
            OutputStreamWriter writer = new OutputStreamWriter(connect.getOutputStream(), "utf-8");
            writer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
            param.writeTo(writer);
            writer.append("</root>");
            writer.flush();
            writer.close();

            ObjectInputStream in = new ObjectInputStream(connect.getInputStream());
            ResultData result = (ResultData) in.readObject();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void send1(ParamData param,DefaultHandler handler) {
        try {
            if (url == null) {
            	url = new URL("http", "localhost", contextpath + "/appletServlet.app;jsessionid=" + jsessionid);
            }
            URLConnection connect = url.openConnection();
            connect.setDoOutput(true);
            connect.setUseCaches(false);
            connect.setRequestProperty("Content-Type", "application/octet-stream");
            OutputStreamWriter writer = new OutputStreamWriter(connect.getOutputStream(), "utf-8");
            writer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
            param.writeTo(writer);
            writer.append("</root>");
            writer.flush();
            writer.close();
        	SAXParserFactory spf = SAXParserFactory.newInstance();
    		SAXParser saxParser = spf.newSAXParser();
    		saxParser.parse(connect.getInputStream(), handler);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
    }
    public static TreeModel getTreeNode(ParamData param) {
    	TreeRead tr = new TreeRead();
    	send1(param, tr);
    	TreeModel model= new DefaultTreeModel(tr.getResult());
        return model;
    }
}
